package com.tvunetworks.center.device.util;

import com.tvunetworks.center.device.model.json.PageParam;
import com.tvunetworks.center.device.model.vo.DeviceVo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import java.util.List;
/**
 * @program:
 * @description:
 * @author HenryChen
 * @create: 9/27/2019 2:33 PM
 **/
public class ExcelUtil {

    /**
     *
     * @param workbook workbook
     * @param sheetNum sheetNum
     * @param sheetTitle sheetTitle
     * @param headers headers
     * @param result result
     * @param pageParam  pageParam
     * @throws Exception 编译异常
     */
    public static void exportExcel(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<DeviceVo>> result, PageParam pageParam) throws Exception {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 38);
        //手动设置列宽
        //   sheet.setColumnWidth(0, 320 * 17 + 184);
        //  sheet.setColumnWidth(3, 390 * 19 + 184);

        XSSFRow rowOne = sheet.createRow(0);
        XSSFCell cellDeviceName = rowOne.createCell((short) 0);
        XSSFCell cellName = rowOne.createCell((short) 1);
        cellDeviceName.setCellValue("Device Name:");
		cellName.setCellValue(pageParam.getReceiverName());
        XSSFRow rowTwo = sheet.createRow(1);
        XSSFCell cellPeerId = rowTwo.createCell((short) 0);
        XSSFCell cellId = rowTwo.createCell((short) 1);
        cellPeerId.setCellValue("PeerID:");
        cellId.setCellValue(pageParam.getPeerId());
        sheet.createRow(2);
        XSSFRow rowFour = sheet.createRow(3);
        XSSFCell cellUnits = rowFour.createCell((short) 0);
        cellUnits.setCellValue("Paired Units:");
        XSSFRow row = sheet.createRow(4);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            setStyle(workbook, cell, 0);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 5;
            for (List<DeviceVo> m : result) {
                for (DeviceVo str : m) {
                    row = sheet.createRow(index);
                    int count = str.getCount();
                    String name = str.getName();
                    String peerId = str.getPeerId();
                    String type = str.getType();
                    XSSFCell cellOne = row.createCell((short) 0);
                    XSSFCell cellTwo = row.createCell((short) 1);
                    XSSFCell cellThree = row.createCell((short) 2);
                    XSSFCell cellFour = row.createCell((short) 3);
                    setStyle(workbook, cellOne, index);
                    setStyle(workbook, cellTwo, index);
                    setStyle(workbook, cellThree, index);
                    setStyle(workbook, cellFour, index);
                    cellOne.setCellValue(count);
                    cellTwo.setCellValue(name);
                    cellThree.setCellValue(peerId);
                    cellFour.setCellValue(type);
                    index++;
                }
            }
        }

    }

    private static void setStyle(XSSFWorkbook workbook, XSSFCell cell, int row) {
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        if (row == 0) {
            // 设置这些样式
            style.setFillForegroundColor(HSSFColorPredefined.PALE_BLUE.getIndex());
            //style.setFillForegroundColor((short) 80);
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.MEDIUM);
            style.setAlignment(HorizontalAlignment.CENTER);
        } else {
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);
            style.setAlignment(HorizontalAlignment.CENTER);
        }
        // 生成一个字体
    /*    XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 10);
        // 把字体应用到当前的样式
        style.setFont(font);*/
        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(style);
    }

}
